<#include "/common/defaultEngine.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
<@header title="设备管理维修记录" libs=["table"]/>

<style>
    table.table-form-title{margin:20px auto 0;}
    table.table-form-title td{text-align: center;border-bottom:0 !important;}
    table.table{width:90%;margin:0 auto;}
    table.table-form-attach{margin-bottom:100px;}
</style>
<div class="row">
    <form id="task-form-complete" action="" method="post">
    <table class="table-form-body table table-hover table-bordered">
        <thead>
        <tr>
            <td colspan="9">
                <button name="btn-insert" type="button" class="btn btn-primary btn-rounded btn-sm">添加</button>
                <!--<button name="btn-copy" type="button" class="btn btn-primary btn-rounded btn-sm">复制行</button>-->
                <button name="btn-del" type="button" class="btn btn-primary btn-rounded btn-sm">删除</button>
                <!--<button name="btn-del-all" type="button" class="btn btn-primary btn-rounded btn-sm">保存</button>-->
                <button type="button" class="btn btn-sm btn-primary" onclick="submitForm()"><i class="fa fa-check"></i><@ctx.i18n text = "保存"/></button>&nbsp;
                <!--<button name="btn-import" type="button" class="btn btn-primary btn-rounded btn-sm">导入数据</button>-->

            </td>
        </tr>
        <tr>
            <th width="10%">序号</th>
            <th>送修日期</th>
            <th>维修日期</th>
            <th>维修费用</th>
            <th>维修状况</th>
            <th>故障原因</th>
            <th>备注</th>

        </tr>
        </thead>

        <#if repair?size gt 0>

        <tbody name="items">
        <#list repair as r>

        <tr data-id="${r.id}">
            <@f.input name="id_${r_index+1}"value="${r.id}"  placeholder="id"  required=true type="hidden"/>
            <td ><span>${r_index+1} </span></td>
            <td><@f.input name="sendTime_${r_index+1}" value="${r.sendTime?string('yyyy-MM-dd HH:mm:ss')}" type="date" dataType="datetime" dataFormat="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择送修日期"  butClass="fa fa-calendar-plus-o" required=true />
            </td>
            <td><@f.input name="repairTime_${r_index+1}" value="${r.repairTime?string('yyyy-MM-dd HH:mm:ss')}" type="date" dataType="datetime" dataFormat="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择维修日期"  butClass="fa fa-calendar-plus-o" required=true />
            </td>

            <td data-num="${r_index+1}"><@f.input name="repairMoney_${r_index+1}" value="${r.repairMoney}" placeholder="维修费用" required=true/></td>

            <td data-num="${r_index+1}"><@f.input name="repairStatus_${r_index+1}" value="${r.repairStatus}" placeholder="维修状况" required=true/></td>

            <td data-num="${r_index+1}"><@f.input name="faultReason_${r_index+1}" value="${r.faultReason}" placeholder="故障原因" required=true/></td>

            <td data-num="${r_index+1}"><@f.input name="remark_${r_index+1}" value="${r.remark}" placeholder="备注" required=true/></td>

        </tr>
        </#list>
        </tbody>
        <#else>
        <tbody name="items">
        <tr data-id="">
            <@f.input name="id_1"value=""  placeholder="id"  required=true type="hidden"/>
            <td ><span>1</span></td>
            <td><@f.input name="sendTime_1" value="" type="date" dataType="datetime" dataFormat="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择送修日期"  butClass="fa fa-calendar-plus-o" required=true />
            </td>
            <td><@f.input name="repairTime_1" value="" type="date" dataType="datetime" dataFormat="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择维修日期"  butClass="fa fa-calendar-plus-o" required=true />
            </td>

            <td ><@f.input name="repairMoney_1" value="" placeholder="维修费用" required=true/></td>

            <td ><@f.input name="repairStatus_1" value="" placeholder="维修状况" required=true/></td>

            <td ><@f.input name="faultReason_1" value="" placeholder="故障原因" required=true/></td>

            <td ><@f.input name="remark_1" value="" placeholder="备注" required=true/></td>


        </tr>
        </tbody>
    </#if>
        <input type="hidden" name="total_items" value="0" />
        <input type="hidden" name="equipmentId" value="${equipment}" />
    </table>
    </form>

</div>
<@footer>
<script type="text/javascript">
    var prefix = baseURL + "bus/equipment";
    $("#task-form-complete").validate({
        focusCleanup: true
    });

    //删除的行的ID信息，以数组形式记录
    var itemsToRemove = [];

    function submitForm() {

        var targets = $("table[name=demo]");
        var totals = targets.children().get(0);
        var lens = $(totals).children().get(0);
        var arrivalAmount=$(lens).children().get(3);
        // var amountLen=$(lens).children().get(3);
        // var claimAmount=$(lens).children().get(1);
        // var amountValue = parseFloat($(claimAmount).find("input").val());
        // var unamountValue = parseFloat($(amountLen).find("input").val());
        console.log("2222");
        console.log(arrivalAmount);
        var sum= parseFloat($(arrivalAmount).find("input").val());

        var target = $("tbody[name=items]");
        // var total = target.children().length;
        //var target = $(this).closest("table").find("tbody");
        var total = target.children().length;
        //var obj = target.children().get(total);
        // var putAmount=0;
        // for(var i=0; i<total; i++){
        //     var line = target.children().get(i);
        //     var len = $(line).children().get(8);
        //     var value = parseFloat($(len).find("input").val());
        //     putAmount=putAmount+value;
        // }

        // alert( putAmount>amountValue);

        $("input[name=total_items]").val(total);
        //$("input[name=arrivalAmount]").val(sum);
        opt.form.submit();
    }

    function submitHandler() {
        if (opt.validate.form()) {
            opt.operate.saveTab(prefix + "/repair", $('#task-form-complete').serialize());
        }
    }

    $("button[name=aaaa]").click(function (e) {
        $("#selectuser222Name").click();
        //e.stopPropagation()
        console.log("aaaa")
    });

    $("button[name=btn-insert]").click(function () {
        var target = $(this).closest("table").find("tbody");
        var indexs=target.children().length; //已有总的行数
        console.log("总共的行数："+indexs);
        var index = indexs + 1; //新插入行的序号
        var tpl = $(target.children().get(0)).prop("outerHTML"); //以第一行为模板进行复制

        tpl = tpl.replaceAll("1", index);
        target.append(tpl);
    });
    $("button[name=btn-del]").click(function () {
        var target = $(this).closest("table").find("tbody");
        var index = target.children().length - 1;
        if(index>0){
            console.log(target.children().get(index))
            var obj = target.children().get(index);
            var dataid = $(obj).attr("data-id");
            console.log(dataid);
            itemsToRemove.push(dataid);
            console.log(itemsToRemove);
            $("input[name=itemsToRemove]").val(itemsToRemove);
            target.children().get(index).remove();
        }
    });
    $("button[name=btn-del-all]").click(function () {
        var target = $(this).closest("table").find("tbody");
        var index = target.children().length - 1;
        for(var i=index; i>0; i--){
            target.children().get(i).remove();
        }
    });
</script>
</@footer>
</@pageTheme>